home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / Hydra11s.lha / HBBS / Source / Test / Config / ConfigFiles.c next >
Text File  |  1996-06-25  |  5KB  |  183 lines

  1. /*
  2.  
  3.   these are how I used to do the config files..  they're much quicker now...
  4.  
  5.  
  6. */
  7.  
  8. V_ERROR  __asm __saveds LIBHBBS_LoadConfig(register __a0 struct CfgFileData *cfgfile, register __a1 UBYTE *filename)
  9. {
  10.   BPTR FH;
  11.   struct FileInfoBlock *FIB=NULL;
  12.   V_ERROR error=ERR_NO_ERROR;
  13.   LONG nr; //num bytes read!
  14.  
  15.   if (!(FH=Open(filename,MODE_OLDFILE)))
  16.   {
  17.     error=LIBHBBS_LogError(BBSGlobal->ErrorLogFile,ERR_ERROR_OPENING,filename,TYPE_FATAL);
  18.   }
  19.   else // opened ok.
  20.   {
  21.     if (FIB=(struct FileInfoBlock *)AllocVec(sizeof(struct FileInfoBlock),MEMF_PUBLIC))
  22.     {
  23.       ExamineFH(FH,FIB);
  24.       cfgfile->Length=FIB->fib_Size;
  25.       if (cfgfile->Data=AllocVec(cfgfile->Length,MEMF_PUBLIC))
  26.       {
  27.         if ((nr=Read(FH,cfgfile->Data,cfgfile->Length))!=cfgfile->Length)
  28.         {
  29.           FreeVec(cfgfile->Data);
  30.           cfgfile->Data=NULL;
  31.           cfgfile->Length=0;
  32.           error=LIBHBBS_LogError(BBSGlobal->ErrorLogFile,ERR_ERROR_READING,filename,TYPE_CRITICAL);
  33.         }
  34.       }
  35.  
  36.       FreeVec(FIB);
  37.     } else error=TYPE_MEMORY;
  38.     Close(FH);
  39.   }
  40.   return(error);
  41. }
  42.  
  43. void  __asm __saveds  LIBHBBS_FlushConfig(register __a0 struct CfgFileData *cfgfile)
  44. {
  45.   if (cfgfile->Data) // just a smidgin of error checking... :-)
  46.   {
  47.     FreeMem(cfgfile->Data,cfgfile->Length);
  48.     cfgfile->Data=NULL;
  49.   }
  50.   cfgfile->Length=0;
  51. }
  52.  
  53. V_SMALLNUM  __asm __saveds LIBHBBS_GetSetting(register __a0 struct CfgFileData *cfgfile,register __a1 void **result,register __d0 V_FLAGS optiontype,register __a2 V_STRING optionstr,register __d1 V_BOOL multi)
  54. {
  55.  
  56.   // Now this is a beauty of a routine!
  57.  
  58.   V_BIGNUM linenum=0;
  59.   V_BOOL done,nearlydone;
  60.   V_SMALLNUM found=0;
  61.   LONG filepos=0,bufferpos;
  62.   V_STRING buffer;
  63.   V_STRING ItemName;
  64.   V_STRING Params;
  65.   V_STRING CompareStr;
  66.   struct Node *node;
  67.  
  68.   if (buffer=AllocMem(BIG_STR,MEMF_PUBLIC))
  69.   {
  70.     if (ItemName=AllocMem(BIG_STR,MEMF_PUBLIC))
  71.     {
  72.       if (Params=AllocMem(BIG_STR,MEMF_PUBLIC))
  73.       {
  74.         if (CompareStr=AllocMem(BIG_STR,MEMF_PUBLIC))
  75.         {
  76.           do
  77.           {
  78.             bufferpos=0;
  79.             done=FALSE;
  80.             nearlydone=FALSE; // :-) love those variable names! :-)
  81.             do
  82.             {
  83.               if (cfgfile->Data[filepos]=='\r' || cfgfile->Data[filepos]=='\n')
  84.               {
  85.                 nearlydone=TRUE;
  86.                 filepos++;
  87.               }
  88.               else
  89.               {
  90.                 if (nearlydone)
  91.                 {
  92.                   done=TRUE;
  93.                 }
  94.                 else
  95.                 {
  96.                   buffer[bufferpos]=cfgfile->Data[filepos];
  97.                   filepos++;
  98.                   bufferpos++;
  99.                 }
  100.               }
  101.             } while (filepos<cfgfile->Length && !done && bufferpos<BIG_STR-2); // 1 for null and 1 more cos we still have to add the null
  102.             buffer[bufferpos]=0;
  103.  
  104.             linenum++;
  105.  
  106.             LIBStripComments(buffer);
  107.             LIBStripSpaces(buffer);
  108.             LIBGetItem(ItemName,buffer);
  109.             LIBGetParams(Params,buffer);
  110.             if (ItemName[0])
  111.             {
  112.               if (multi==OPT_MULTI)
  113.               {
  114.                 sprintf(CompareStr,"%s_%d",optionstr,found+1);
  115.               }
  116.               else strcpy(CompareStr,optionstr);
  117.  
  118.  
  119.               if (stricmp(CompareStr,ItemName)==0)
  120.               {
  121.                 switch(optiontype)
  122.                 {
  123.                   case VTYPE_STRING:
  124.                     if (*result!=NULL) LIBFreeStr(*result);
  125.                     *result=(V_STRING *)LIBDupStr(Params);
  126.                     found++;
  127.                     break;
  128.                   case VTYPE_BOOL:
  129.                     *result=(V_BOOL *)LIBCheckBoolean(Params);
  130.                     found++;
  131.                     break;
  132.                   case VTYPE_BIGNUM:
  133.                     *result=(V_BIGNUM *)atoi(Params);
  134.                     found++;
  135.                     break;
  136.                   case VTYPE_SMALLNUM:
  137.                     *result=(V_SMALLNUM *)atoi(Params);
  138.                     found++;
  139.                     break;
  140.                   case VTYPE_STRINGLIST:
  141.                     // free existing stringlist if present
  142.  
  143.                     if ((found==0) && (*result))
  144.                     {
  145.                       LIBFreeStrList((V_STRINGLIST)*result);
  146.                     }
  147.                     if (!(*result))
  148.                     {
  149.                       if(*result=AllocMem(sizeof (struct List),MEMF_PUBLIC))
  150.                       {
  151.                         NewList((struct List*)*result);
  152.                       }
  153.                     }
  154.                     if (*result) // allocated new list ok ?
  155.                     {
  156.                       if (node=AllocMem(sizeof(struct Node),MEMF_PUBLIC))
  157.                       {
  158.                         if (!(node->ln_Name=LIBDupStr(Params)))
  159.                         {
  160.                           FreeMem(node,sizeof(struct Node));
  161.                         }
  162.                         else
  163.                         {
  164.                           AddTail((struct List *)*result,node);
  165.                           found++;
  166.                         }
  167.                       }
  168.                     }
  169.                 }
  170.               }
  171.             }
  172.           } while (filepos<cfgfile->Length && ((multi==OPT_SINGLE && !found) || (multi==OPT_MULTI)));
  173.           FreeMem(CompareStr,BIG_STR);
  174.         }
  175.         FreeMem(Params,BIG_STR);
  176.       }
  177.       FreeMem(ItemName,BIG_STR);
  178.     }
  179.     FreeMem(buffer,BIG_STR);
  180.   }
  181.   return(found);
  182. }
  183.